package com.ly.user_system.base;


import com.ly.user_system.common.PageResult;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HASEE
 */
public class BaseController<Service extends BaseService> {

    protected Service baseService;

    @Autowired
    public void setBaseService(Service baseService) {
        this.baseService = baseService;
    }

    @PostMapping("/save")
    public<DTO> Boolean create(@RequestBody DTO dto) {
        return baseService.saveBatch(List.of(dto));
    }

    @PostMapping("/save/batch")
    public<DTO> Boolean createBatch(@RequestBody List<DTO> list) {
        return baseService.saveBatch(list);
    }

    @PutMapping
    public <DTO>Boolean update(@RequestBody  DTO dto) {
        return baseService.update(dto);
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Long id) {
        return baseService.deleteBatch(List.of(id));
    }

    @DeleteMapping("/batch")
    public Boolean deleteBatch(@RequestBody List<Long> ids) {
        return baseService.deleteBatch(ids);
    }

    @GetMapping("/{id}")
    public<DTO> DTO getById(@PathVariable Long id) {
        return (DTO) baseService.getById(id);
    }

    @PostMapping("/page")
    public<DTO,PQuery extends PageQuery> PageResult<DTO> page(@RequestBody  @Valid PQuery query) {
        return baseService.page(query);
    }


    @PostMapping("/list")
    public<DTO> List<DTO> page(@RequestBody  DTO dto) {
        return baseService.list(dto);
    }




}
